#include "RequestHandler.h"
#include "MessageQueue.h"
#include "Trace.h"


namespace tsc
{
namespace common
{

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
RequestHandler::RequestHandler()
{
}

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
RequestHandler::~RequestHandler()
{

}

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
requestToQueue_e RequestHandler::putRequestOnInQueue(Message*& aMessageP)
{
  ENTER("RequestHandler::putRequestOnInQueue");

  MessageQueue::instance()->toInQueue(aMessageP);

  RETURN(REQUEST_IS_ON_QUEUE);
} // RequestHandler::putRequestOnInQueue

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
requestToQueue_e RequestHandler::putRequestOnOutQueue(Message*& aMessageP)
{
  ENTER("RequestHandler::putRequestOnOutQueue");

  MessageQueue::instance()->toOutQueue(aMessageP);

  RETURN(REQUEST_IS_ON_QUEUE);
} // RequestHandler::putRequestOnOutQueue

replyFromQueue_e getReplyFromOutQueue(Message*& aMessageP,
                                      unsigned int aConnectionId = 0);

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
replyFromQueue_e RequestHandler::getReplyFromInQueue(
    Message*&    aMessageP,
    unsigned int aConnectionId)
{
  ENTER("RequestHandler::getReplyFromInQueue");

  bool result;

  if (aConnectionId)
  {
    result = MessageQueue::instance()->fromInQueue(aMessageP, aConnectionId);
  }
  else
  {
    result = MessageQueue::instance()->fromInQueue(aMessageP);
  }

  if (result)
  {
    RETURN(REPLY_PRESENT);
  }
  else
  {
    RETURN(REPLY_QUEUE_IS_EMPTY);
  }

} // RequestHandler::getReplyFromInQueue

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
replyFromQueue_e RequestHandler::getReplyFromOutQueue(
    Message*&    aMessageP,
    unsigned int aConnectionId)
{
  ENTER("RequestHandler::getReplyFromOutQueue");

  bool result;

  if (aConnectionId)
  {
    result = MessageQueue::instance()->fromOutQueue(aMessageP, aConnectionId);
  }
  else
  {
    result = MessageQueue::instance()->fromOutQueue(aMessageP);
  }

  if (result)
  {
    RETURN(REPLY_PRESENT);
  }
  else
  {
    RETURN(REPLY_QUEUE_IS_EMPTY);
  }
} // RequestHandler::getReplyFromOutQueue

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
unsigned int RequestHandler::startTimer(Message*& aMessageP, unsigned long aTimeout)
{
  ENTER("RequestHandler::startTimer");

  MessageQueue::instance()->toTimerQueue(aMessageP);

  TRACE(2, STR("Starting timer Id=0x%08X, with timeout %u ms.",
               aMessageP->getId(), aTimeout));

  //TODO create deadline_timer

  RETURN(aMessageP->getId());
} // RequestHandler::startTimer

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
void RequestHandler::timerExpired(unsigned int aTimerId)
{
  ENTER("RequestHandler::timerExpired");

  Message* pMessage = 0;

  // put the expired timer message in the IN queue
  if(MessageQueue::instance()->fromTimerQueue(pMessage, aTimerId))
  {
    TRACE(2, STR("Timer Id=0x%08X expired.",
                 aTimerId));
    MessageQueue::instance()->toInQueue(pMessage);
  }
  else
  {
    TRACE_ERROR(STR("Timer Id=0x%08X expired, but there is no associated Message with it.",
                    aTimerId));
  }

  RETURN;
} //RequestHandler::timerExpired

/*******************************************************************************
 *
 *
 *
 ******************************************************************************/
void RequestHandler::cancelTimer(unsigned int aTimerId)
{
  ENTER("RequestHandler::timerExpired");

  Message* pMessage = 0;

  //TODO cancel deadline_timer

  if(MessageQueue::instance()->fromTimerQueue(pMessage, aTimerId))
  {
    TRACE(2, STR("Timer Id=0x%08X cancelled.",
                 aTimerId));
    delete pMessage;
    pMessage = 0;
  }
  else
  {
    TRACE_ERROR(STR("Timer Id=0x%08X cancelled, but there is no associated Message with it.",
                    aTimerId));
  }

  RETURN;
} // RequestHandler::cancelTimer

} // namespace common
} // namespace tsc
